var baseUrl='https://fxtravel.ncscloudmedia.com/index.php'// 线上
// var baseUrl='http://192.168.1.108/fx_cultural_tour/public/index.php'//本地

// token验证接口
const tokenApi = "/api/user/checkToken";
// 登录页地址
const loginUrl = "/pages/login/wxLogin";
/**
 * get请求
 * */ 
function getRequest(url,data={},fun){
  /*wx.showLoading({
    title: "正在加载中...",
    mask:true
  })*/
  wx.request({
    url:baseUrl+url,
    header:{
      'content-type':'application/json'
    },
    method:'GET',
    data:data,
    success(res){
      //wx.hideLoading();
      return typeof fun=='function'&&fun(res.data) 
    },
    fail(res){
      //wx.hideLoading();
      wx.showModal({
        title: '网络错误',
        content: '网络出错，请刷新重试1',
        showCancel: false
      })
      return typeof fun == "function" && fun(false)
    }
  })
}

/**
 * post请求
 **/ 
function postRequest(url,data={},fun){
  wx.showLoading({
    title: "正在加载中...",
  })
  // data.token=wx.getStorageSync('tokenD');
  let token = wx.getStorageSync('token')
  wx.request({
    url:baseUrl+url,
    header:{
      'content-type':'application/json; charset=utf-8',
      'token':token 
    },
    data:data,
    method:'POST',
    success(res){
      wx.hideLoading(); 
      return typeof fun=='function'&&fun(res.data)
    },
    fail(res){
      wx.hideLoading();
      wx.showModal({
        title: '网络错误',
        content: '网络出错，请刷新重试2',
        showCancel: false
      })
      return typeof fun=='function'&&fun(false)
    }
  })
  
}
function PostRequest(url,data={},fun){
  // let token = 'vNTT8GGmHHZANDJgFRonE917DOkpggZb'
  let token = wx.getStorageSync('token')
  // if(token){
    wx.request({
      url:baseUrl+url,
      header:{
        'content-type':'application/json; charset=utf-8',
        'token':token
      },
      data:data,
      method:'POST',
      success(res){
        let code = res.data.code
        // return typeof fun=='function'&&fun(res.data)
        if(code==20003||code==10000){
          wx.redirectTo({
            url: '/pages/login/wxLogin',
          })
        }else{
          return typeof fun=='function'&&fun(res.data)
        }
      },
      fail(res){
        wx.showModal({
          title: '网络错误',
          content: '网络出错，请刷新重试',
          showCancel: false
        })
        return typeof fun=='function'&&fun(false)
      }
    })
  // }else{
    // wx.navigateTo({
    //   url: loginUrl,
    // })
  // }

}
function PostRequest2(url,data={},fun){
  // let token = 'vNTT8GGmHHZANDJgFRonE917DOkpggZb'
  let token = wx.getStorageSync('token2')
  // if(token){
    wx.request({
      url:baseUrl+url,
      header:{
        'content-type':'application/json; charset=utf-8',
        'token':token
      },
      data:data,
      method:'POST',
      success(res){
        let code = res.data.code
        // return typeof fun=='function'&&fun(res.data)
        if(code==70003||code==10000){
          wx.redirectTo({
            url: '/pages/login/index',
          })
        }else{
          return typeof fun=='function'&&fun(res.data)
        }
      },
      fail(res){
        wx.showModal({
          title: '网络错误',
          content: '网络出错，请刷新重试',
          showCancel: false
        })
        return typeof fun=='function'&&fun(false)
      }
    })
  // }else{
    // wx.navigateTo({
    //   url: loginUrl,
    // })
  // }

}
function upLoadRequest(url,file={},fun){
  wx.uploadFile({
    url:baseUrl+url,
    filePath: file.url,
    name: 'file',
    success(res){
      //wx.hideLoading();
      return typeof fun=='function'&&fun(JSON.parse(res.data))
    },
    fail(res){
      //wx.hideLoading();
      wx.showModal({
        title: '网络错误',
        content: '网络出错，请刷新重试2',
        showCancel: false
      })
      return typeof fun=='function'&&fun(false)
    }
  })
}

module.exports={
  getRequest:getRequest,
  postRequest:postRequest,
  PostRequest:PostRequest,
  PostRequest2:PostRequest2,
  upLoadRequest:upLoadRequest
}